﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Text;
using System.Threading.Tasks;
using ZFine.Code;
using ZFine.Domain.Entity;
using ZFine.Domain.IRepository.Project;
using ZFine.Repository.Project;
namespace ZFine.Application
{
    public class CourseTypeApp
    {
       private ICourseTypeRepository service = new CourseTypeRepository();

       public List<CourseTypeEntity> GetList()
       {
           return service.IQueryable().ToList();
       }
       public List<CourseTypeEntity> GetList(Expression<Func<CourseTypeEntity, bool>> predicate)
       {
           return service.IQueryable(predicate).ToList();
       }

       public List<CourseTypeEntity> GetList(Pagination pagination, string keyword)
       {
           var expression = ExtLinq.True<CourseTypeEntity>();
           if (!string.IsNullOrEmpty(keyword))
           {
               expression = expression.And(t => t.ClassName.Contains(keyword));
           }
           expression = expression.And(t => t.State == true);
           return service.FindList(expression, pagination);
       }
       public CourseTypeEntity GetForm(string keyValue)
       {
           return service.FindEntity(keyValue);
       }
       public int DeleteForm(string keyValue)
       {
           var local = service.FindEntity(keyValue);
           if (local != null)
           {
               local.State = false;
               return service.Update(local);
           }
           else
           {
               return -1;
           }
       }
       public int SubmitForm(CourseTypeEntity ServiceEntity, string keyValue)
       {
           var LoginInfo = OperatorProvider.Provider.GetCurrent();
           if (LoginInfo != null)
           {
               if (!string.IsNullOrEmpty(keyValue))
               {
                   if (service.IQueryable(f => f.ClassName == ServiceEntity.ClassName && f.State == true).FirstOrDefault() != null)
                   {
                       return -2;
                   }
                   ServiceEntity.Modify(keyValue);
                   if (ServiceEntity.AddUserName == null)
                   {
                       ServiceEntity.AddUserName = LoginInfo.UserName;
                   }
                   return service.Update(ServiceEntity);
               }
               else
               {
                   if (service.IQueryable(f => f.ClassName == ServiceEntity.ClassName && f.State == true).FirstOrDefault() != null)
                   {
                       return -2;
                   }
                   ServiceEntity.Create();
                   ServiceEntity.State = true;
                   if (ServiceEntity.AddUserName == null)
                   {
                       ServiceEntity.AddUserName = LoginInfo.UserName;
                   }
                   return service.Insert(ServiceEntity);
               }
           }
           return -1;
       }
    }
}
